package br3akr.DeadlyDinnerParty.Server.Actions;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import br3akr.DeadlyDinnerParty.Server.Engine.Server;
import br3akr.DeadlyDinnerParty.Server.Engine.Session;
import br3akr.DeadlyDinnerParty.Server.Exceptions.DeadlyDinnerPartyException;
import br3akr.DeadlyDinnerParty.Server.World.Area;
import br3akr.DeadlyDinnerParty.Server.World.GameCharacter;

public class ScryAction extends Action implements MultiStepAction {
	private static final int START  = 1;
	private static final int SELECT_ROOM = 2;
	private static final int SCRYING = 3;
	private int curStep;
	private String currentDescription;
	private ActionResponse actionResponse;
	private boolean actionComplete;
	private Area scryLocation;
	
	public static final Set<String> keywords;
	static
	{
		Set<String>tempKeywords = new HashSet<String>();
		tempKeywords.add("Scry");
		keywords = Collections.unmodifiableSet(tempKeywords);
	}
    
    public ScryAction() {
        super("Scry", false);
		curStep = 1;
		actionComplete = false;
    }
    
    public ActionResponse doAction(String action, Session session) throws DeadlyDinnerPartyException {
		if (session == null) {
			actionComplete = true;
			throw new DeadlyDinnerPartyException ("No session found.");
		}
		
		GameCharacter actor = session.getCharacter();
		Area location = actor.getLocation();
		Set<GameCharacter> localOnlookers = location.getAreaOnlookers(session.getCharacter(), null);
		
		if (actionResponse != null) {
			actionResponse.clearOnlookers();
		}
		
		switch (curStep) {
			case START:
				actionResponse = new ActionResponse();
				actionResponse.addAffectedArea(location);
				actionResponse.setActingCharacter(actor);
				StringBuilder sb = new StringBuilder("Which room would you like to scry on?");
				for (Area a : Server.world.getAreas()) {
					sb.append("\n" + a.getName());
				}
				actionResponse.setActingCharacterDescription(sb.toString());
				actionResponse.addOnlookers(localOnlookers, actor.getName() + "'s eyes roll into the back of their head, and begins humming and muttering inhuman words.");
				curStep = SELECT_ROOM;
				currentDescription = actor.getName() + "'s eyes are rolled into the back of their head and they're humming and muttering inhuman words.";
				break;
			case SELECT_ROOM:
				scryLocation = Server.world.getArea(action);
				scryLocation.addScryer(actor);
				actionResponse.setActingCharacterDescription("You begin scrying on " + scryLocation.getName());
				actionResponse.addOnlookers(localOnlookers, actor.getName() + "'s eyes suddenly snap open, and their jaw hangs open wide. They stair off into the distance as if not seeing anything here.");
				currentDescription = actor.getName() + "'s eyes are open freakishly, and their jaw hangs open wide. They stair off into the distance as if not seeing anything here.";
                curStep = SCRYING;
				break;
			case SCRYING:
				actionResponse.setActingCharacterDescription("You stop scrying.");
				actionResponse.addOnlookers(localOnlookers, actor.getName() + " suddenly relaxes, and returns to the room from wherever they were.");
				actionComplete = true;
				break;
			default:
				break;
		}
        
        return actionResponse;
    }
    
	public void reset() {
		actionResponse = null;
		curStep = 1;
		actionComplete = false;
	}
	
    public ActionResponse getActionResponse() {
		return actionResponse;
	}

	public boolean isComplete() {
		return actionComplete;
	}
    
    public Set<String> getKeywords() {
        return keywords;
    }

	@Override
	public String getCurrentDescription() {
		return currentDescription;
	}
}